import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '', // 初始化token
    permissionList: '',
    roleName: '',
    userId: '',
    isCollapse: '',
    username: '',
    userEmail: '',
    userMobile: '',
    userNumber: '',
    newuser: '',
    at: [],
    ct: [],
    department: [],
    level: [],
    nature: [],
    pt: [],
    pr: [],
    ps: [],
    periodical: [],
    rt: [],
    status: [],
    sc: [],
    subject: [],
    user: [],
    applynum: '',
    firstnum: '',
    secondnum: '',
    endapplynum: '',
    endnum: '',
    projectlistlength: ''
  },
  mutations: {
    // 存储token方法
    // 设置token等于外部传递进来的值
    setToken(state, token) {
      state.token = token
      window.sessionStorage.token = token // 同步存储token至sessionStorage
    },

    setpermissionList(state, permissionList) {
      state.permissionList = permissionList
      window.sessionStorage.permissionList = permissionList
    },

    setnewuser(state, newuser) {
      state.newuser = newuser
      window.sessionStorage.newuser = newuser
    },

    setroleName(state, roleName) {
      state.roleName = roleName
      window.sessionStorage.roleName = roleName
    },

    setuserId(state, userId) {
      state.userId = userId
      window.sessionStorage.userId = userId
    },

    setisCollapse(state, isCollapse) {
      state.isCollapse = isCollapse
      window.sessionStorage.isCollapse = isCollapse
    },

    setusername(state, username) {
      state.username = username
      window.sessionStorage.username = username
    },

    setuserEmail(state, userEmail) {
      state.userEmail = userEmail
      window.sessionStorage.userEmail = userEmail
    },

    setuserMobile(state, userMobile) {
      state.userMobile = userMobile
      window.sessionStorage.userMobile = userMobile
    },

    setuserNumber(state, userNumber) {
      state.userNumber = userNumber
      window.sessionStorage.userNumber = userNumber
    },

    setat(state, at) {
      state.at = at
      window.sessionStorage.at = at
    },

    setct(state, ct) {
      state.ct = ct
      window.sessionStorage.ct = ct
    },

    setdepartment(state, department) {
      state.department = department
      window.sessionStorage.department = department
    },

    setlevel(state, level) {
      state.level = level
      window.sessionStorage.level = level
    },

    setnature(state, nature) {
      state.nature = nature
      window.sessionStorage.nature = nature
    },

    setpt(state, pt) {
      state.pt = pt
      window.sessionStorage.pt = pt
    },

    setpr(state, pr) {
      state.pr = pr
      window.sessionStorage.pr = pr
    },

    setps(state, ps) {
      state.ps = ps
      window.sessionStorage.ps = ps
    },

    setperiodical(state, periodical) {
      state.periodical = periodical
      window.sessionStorage.periodical = periodical
    },

    setrt(state, rt) {
      state.rt = rt
      window.sessionStorage.rt = rt
    },

    setstatus(state, status) {
      state.status = status
      window.sessionStorage.status = status
    },

    setsc(state, sc) {
      state.sc = sc
      window.sessionStorage.sc = sc
    },

    setsubject(state, subject) {
      state.subject = subject
      window.sessionStorage.subject = subject
    },

    setuser(state, user) {
      state.user = user
      window.sessionStorage.user = user
    },

    setapplynum(state, applynum) {
      state.applynum = applynum
      window.sessionStorage.applynum = applynum
    },

    setfirstnum(state, firstnum) {
      state.firstnum = firstnum
      window.sessionStorage.firstnum = firstnum
    },

    setsecondnum(state, secondnum) {
      state.secondnum = secondnum
      window.sessionStorage.secondnum = secondnum
    },

    setendapplynum(state, endapplynum) {
      state.endapplynum = endapplynum
      window.sessionStorage.endapplynum = endapplynum
    },

    setendnum(state, endnum) {
      state.endnum = endnum
      window.sessionStorage.endnum = endnum
    },

    setprojectlistlength(state, projectlistlength) {
      state.projectlistlength = projectlistlength
      window.sessionStorage.projectlistlength = projectlistlength
    }

  },
  getters: {
    // 获取token方法
    // 判断是否有token,如果没有重新赋值，返回给state的token
    getToken(state) {
      if (!state.token) {
        state.token = window.sessionStorage.getItem('token')
      }
      return state.token
    },

    getpermissionList(state) {
      if (!state.permissionList) {
        state.permissionList = window.sessionStorage.getItem('permissionList')
      }
      return JSON.parse(state.permissionList)
    },

    getroleName(state) {
      if (!state.roleName) {
        state.roleName = window.sessionStorage.getItem('roleName')
      }
      return state.roleName
    },

    getuserId(state) {
      if (!state.userId) {
        state.userId = window.sessionStorage.getItem('userId')
      }
      return state.userId
    },

    getnewuser(state) {
      if (!state.newuser) {
        state.newuser = window.sessionStorage.getItem('newuser')
      }
      return state.newuser
    },

    getisCollapse(state) {
      if (!state.isCollapse) {
        state.isCollapse = window.sessionStorage.getItem('isCollapse')
      }
      return JSON.parse(state.isCollapse)
    },

    getusername(state) {
      if (!state.username) {
        state.username = window.sessionStorage.getItem('username')
      }
      return (state.username)
    },

    getuserEmail(state) {
      if (!state.userEmail) {
        state.userEmail = window.sessionStorage.getItem('userEmail')
      }
      return (state.userEmail)
    },

    getuserMobile(state) {
      if (!state.userMobile) {
        state.userMobile = window.sessionStorage.getItem('userMobile')
      }
      return (state.userMobile)
    },

    getuserNumber(state) {
      if (!state.userNumber) {
        state.userNumber = window.sessionStorage.getItem('userNumber')
      }
      return (state.userNumber)
    },

    getat(state) {
      if (state.at.length === 0) {
        state.at = window.sessionStorage.getItem('at')
      }
      return (state.at)
    },

    getct(state) {
      if (state.ct.length === 0) {
        state.ct = window.sessionStorage.getItem('ct')
      }
      return (state.ct)
    },

    getdepartment(state) {
      if (state.department.length === 0) {
        state.department = window.sessionStorage.getItem('department')
      }
      return (state.department)
    },

    getlevel(state) {
      if (state.level.length === 0) {
        state.level = window.sessionStorage.getItem('level')
      }
      return (state.level)
    },

    getnature(state) {
      if (state.nature.length === 0) {
        state.nature = window.sessionStorage.getItem('nature')
      }
      return (state.nature)
    },

    getpt(state) {
      if (state.pt.length === 0) {
        state.pt = window.sessionStorage.getItem('pt')
      }
      return (state.pt)
    },

    getpr(state) {
      if (state.pr.length === 0) {
        state.pr = window.sessionStorage.getItem('pr')
      }
      return (state.pr)
    },

    getps(state) {
      if (state.ps.length === 0) {
        state.ps = window.sessionStorage.getItem('ps')
      }
      return (state.ps)
    },

    getperiodical(state) {
      if (state.periodical.length === 0) {
        state.periodical = window.sessionStorage.getItem('periodical')
      }
      return (state.periodical)
    },

    getrt(state) {
      if (state.rt.length === 0) {
        state.rt = window.sessionStorage.getItem('rt')
      }
      return (state.rt)
    },

    getstatus(state) {
      if (state.status.length === 0) {
        state.status = window.sessionStorage.getItem('status')
      }
      return (state.status)
    },

    getsc(state) {
      if (state.sc.length === 0) {
        state.sc = window.sessionStorage.getItem('sc')
      }
      return (state.sc)
    },

    getsubject(state) {
      if (state.subject.length === 0) {
        state.subject = window.sessionStorage.getItem('subject')
      }
      return (state.subject)
    },

    getuser(state) {
      if (state.user.length === 0 || state.user === null) {
        state.user = window.sessionStorage.getItem('user')
      }
      return (state.user)
    },

    getapplynum(state) {
      if (state.applynum.length === 0) {
        state.applynum = window.sessionStorage.getItem('applynum')
      }
      return (state.applynum)
    },

    getfirstnum(state) {
      if (state.firstnum.length === 0) {
        state.firstnum = window.sessionStorage.getItem('firstnum')
      }
      return (state.firstnum)
    },

    getsecondnum(state) {
      if (state.secondnum.length === 0) {
        state.secondnum = window.sessionStorage.getItem('secondnum')
      }
      return (state.secondnum)
    },

    getendapplynum(state) {
      if (state.endapplynum.length === 0) {
        state.endapplynum = window.sessionStorage.getItem('endapplynum')
      }
      return (state.endapplynum)
    },

    getendnum(state) {
      if (state.endnum.length === 0) {
        state.endnum = window.sessionStorage.getItem('endnum')
      }
      return (state.endnum)
    },

    getprojectlistlength(state) {
      if (state.projectlistlength.length === 0) {
        state.projectlistlength = window.sessionStorage.getItem('projectlistlength')
      }
      return (state.projectlistlength)
    }
  }
})
